﻿#include "stdafx.h"
#include "AstImport.h"
#include "AstPackage.h"
#include "BlockContext.h"

extern AstPackage* currentPackage;
//std::map<std::string, AstPackage*> packages;

extern int yyparse(void);
extern FILE *yyin;

CodeGen * AstImport::makeGen(SiContext * parent)
{
	auto *old = currentPackage;

	currentPackage = new AstPackage();

	std::string className = identifiers.back();
	std::string n;
	for (auto i : identifiers) {
		n += i + "/";
	}
	n.pop_back();

	// packages[n] = currentPackage;
	
	std::string fname = "lib/" + n + ".si";
	yyin = fopen(fname.c_str(), "r"); /* 首先打开要被处理的文件（参数1）yyin是lex默认的文件输入指针，设置了则不处理控制台输入 */
	if (!yyin) {
			throw std::runtime_error("无法打开文件：" + fname);
	}
	if (0 != yyparse()) {
		throw new std::runtime_error("解析" + fname + "异常");
	}
	fclose(yyin);

	auto* importedPackage = currentPackage;
	currentPackage = old;

	auto *x=importedPackage->preprocessor();
	auto *cls=x->findClass(className);
	parent->setClass(className, cls);
	return nullptr;
}
